package com.dongdong.timeer;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class text {

    public static void main(String[] args) {


        try {   //防止控制台乱码
            System.setOut(new PrintStream(System.out, true, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        System.out.println("开始测试");


/*//1.Timer的一次性定是任务 schedule
        System.out.println("项目启动时间"+new Date().toLocaleString());
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            private int i=0;
            @Override
            public void run() {
                System.out.println("timer任务开始执行"+new Date().toLocaleString());
                if(i==2) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                i++;
                System.out.println("timeer任务执行结束"+new Date().toLocaleString());

            }},  2000);*/

/*//2.Timer的循环定时任务 scheduleAtFixedRate

        System.out.println("项目启动时间"+new Date().toLocaleString());
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("scheduleAtFixedRate任务开始执行"+new Date().toLocaleString());
            }
            },  0,  2000);*/

/*
//3.ScheduleThreadPool的一次性定时任务schedule
        //定义多线程 设置线程次数为五
        System.out. println("项目启动时间"+new Date().toLocaleString());
        ScheduledExecutorService service = Executors. newScheduledThreadPool(5);
        // 只执行一次
        service. schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule任务开始执行" + new Date().toLocaleString());
            }
            // 2：初始化后多长时间执行 3：时间单位
        }, 0, TimeUnit.SECONDS);
*/
/*//4.ScheduleThreadPool的循环定时任务scheduleAtFixedRate
        //定义多线程 设置线程次数为五
        System.out. println("项目启动时间"+new Date().toLocaleString());
        ScheduledExecutorService service = Executors. newScheduledThreadPool(5);
        // 只执行一次
        service. scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduleAtFixedRate任务开始执行" + new Date().toLocaleString());
            }
            // 2：初始化后多长时间执行 3：时间单位
        }, 0, 2,TimeUnit.SECONDS);*/

//5.顺延
        //定义多线程 设置线程次数为五
        System.out. println("项目启动时间"+new Date().toLocaleString());
        ScheduledExecutorService service = Executors. newScheduledThreadPool(5);
        // 只执行一次
        service. scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduleAtFixedRate任务开始执行" + new Date().toLocaleString());
            }
            // 2：初始化后多长时间执行 3：时间单位
        }, 0, 2,TimeUnit.SECONDS);












    }
















}
